import on, { IHandle } from '@feidao-factory/core/on';

const reg = /\s*([^\s:]*)\s*:\s*([^:\s]*)\s*/;
const FD_ACTIONS_FLAG = 'data-feidao-actions';

export default function init_events(emit: (event: string, ...args: any[]) => Promise<any> | undefined) {
	return (node: HTMLElement | DocumentFragment) => {
		const ns = Array.from(node.querySelectorAll('[' + FD_ACTIONS_FLAG + ']')) as HTMLElement[];
		if ((node as HTMLElement).getAttribute) {
			ns.push(node as HTMLElement);
		}
		return ns.reduce((handles, n) => {
			const attr = n.getAttribute(FD_ACTIONS_FLAG);
			if (attr) {
				const events = attr.split(',');
				return handles.concat(events.map((e) => {
					const m = e.match(reg);
					if (m && m.length === 3) {
						const evt = m[1];
						const event = m[2];
						return on(n, evt, (_e) => {
							emit(event, _e);
						});
					} else {
						return null as any as IHandle;
					}
				}).filter((h) => {
					return !!h;
				}));
			} else {
				return handles;
			}
		}, [] as IHandle[]);
	};
}
